﻿#region

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using FluentValidation.Mvc;
using HouseHoldBudget.Core.Infrastructure;
using HouseHoldBudget.Framework.Mvc;
using HouseHoldBudget.Framework.Mvc.Engine;
using HouseHoldBudget.Infrastructure;

#endregion

namespace HouseHoldBudget
{
    public class MvcApplication : HttpApplication
    {
        #region Instance Methods

        protected void Application_Start()
        {
            //initialize engine context
            EngineContext.Initialize(false);

            //set dependency resolver
            var dependencyResolver = new CustomDependencyResolver();
            DependencyResolver.SetResolver(dependencyResolver);

            //model binders
            ModelBinders.Binders.Add(typeof (MvcModelBase), new ModelBinder());

            //remove all view engines
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new BudgetViewEngine());

            //Mapper
            AutoMapperStartupTask.Configure();

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //FLuent Validator
            DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
            ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new ValidatorFactory()));
        }

        #endregion

        #region Class Methods

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = UrlParameter.Optional},
                new[] {"HouseHoldBudget.Controllers"}
                );
        }

        #endregion
    }
}